package main

import (
	"fmt"
	"time"
)

//select实现超时
func main() {
	fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
	ch := make(chan int)
	quit := make(chan bool)

	go func() {
		for {
			select {
			case num := <-ch:
				//当channel持续3s没有数据写入时,触发超时
				fmt.Println(num)
			case <-time.After(3 * time.Second):
				fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
				fmt.Println("超时")
				quit<-true
			}
		}
	}()

	for i:=0; i < 5; i++ {
		ch<-i
		time.Sleep(time.Second)
	}

	<-quit
	fmt.Println("程序结束")
}
